package com.ncd.form.controller;

import com.ncd.common.annotation.Log;
import com.ncd.common.core.controller.BaseController;
import com.ncd.common.core.domain.AjaxResult;
import com.ncd.common.core.page.TableDataInfo;
import com.ncd.common.core.text.Convert;
import com.ncd.common.enums.BusinessType;
import com.ncd.form.domain.LayoutVersion;
import com.ncd.form.service.ILayoutVersionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 单历史版本Controller
 *
 * @author treenewbee
 * @date 2019-11-01
 */
@Controller
@RequestMapping("/form/version")
public class LayoutVersionController extends BaseController
{
    private String prefix = "form/version";

    @Autowired
    private ILayoutVersionService layoutVersionService;

    @RequiresPermissions("form:version:view")
    @GetMapping()
    public String version()
    {
        return prefix + "/version";
    }

    /**
     * 查询单历史版本列表
     */
    @RequiresPermissions("form:version:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LayoutVersion layoutVersion)
    {
        startPage();
        List<LayoutVersion> list = layoutVersionService.queryList(layoutVersion);
        return getDataTable(list);
    }



    /**
     * 新增单历史版本
     */
    @GetMapping("/add/{layoutId}")
    public String add(@PathVariable("layoutId") Long layoutId, ModelMap mmap)
    {
        mmap.put("layoutId",layoutId);
        return prefix + "/add";
    }

    /**
     * 新增保存单历史版本
     */
    @RequiresPermissions("form:version:add")
    @Log(title = "单历史版本", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LayoutVersion layoutVersion)
    {
        Long version=layoutVersionService.selectMaxVersion(layoutVersion.getLayoutId());
        layoutVersion.setVersionNum(version+1);
        return toAjax(layoutVersionService.save(layoutVersion));
    }

    /**
     * 修改单历史版本
     */
    @GetMapping("/edit/{versionId}")
    public String edit(@PathVariable("versionId") Long versionId, ModelMap mmap)
    {
        LayoutVersion layoutVersion = layoutVersionService.getById(versionId);
        mmap.put("layoutVersion", layoutVersion);
        return prefix + "/edit";
    }

    /**
     * 修改保存单历史版本
     */
    @RequiresPermissions("form:version:edit")
    @Log(title = "单历史版本", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LayoutVersion layoutVersion)
    {
        return toAjax(layoutVersionService.updateById(layoutVersion));
    }

    /**
     * 删除单历史版本
     */
    @RequiresPermissions("form:version:remove")
    @Log(title = "单历史版本", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(layoutVersionService.removeByIds(Convert.toLongList(ids)));
    }

    /**
     * 查询单历史版本
     */
    @GetMapping( "/getActiveVersion/{layoutId}")
    @ResponseBody
    public AjaxResult getActiveVersion(@PathVariable("layoutId") Long layoutId)
    {
        LayoutVersion layoutVersion=layoutVersionService.selectActiveLayoutVersion(layoutId);
        if(layoutVersion==null){
            return AjaxResult.error();
        }else{
            return AjaxResult.success(layoutVersion);
        }

    }
}

